package cn.org.timespace.ons.dao.policy.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import cn.org.timespace.ons.action.policy.DataContentsModel;
import cn.org.timespace.ons.dao.TSBaseDAO;
import cn.org.timespace.ons.dao.policy.DataContentsDao;
import cn.org.timespace.ons.util.pager.Pager;
import cn.org.timespace.ons.vo.base.DataContents;
import cn.org.timespace.ons.vo.base.Mom;

public class DataContentsDaoImpl  extends TSBaseDAO implements DataContentsDao{
	@Override
	public DataContents findById(String id) {
			// TODO Auto-generated method stub
			log.debug("getting DataContents instance with id: " +id);
			try{
				DataContents instance = (DataContents)getHibernateTemplate().get(
						"cn.org.timespace.ons.vo.base.DataContents", id);
				return instance;
			}catch(RuntimeException re){
				log.debug("get fail",re);
				throw re;
		}
	}
	
	@Override
	public void save(DataContents instance) {
	
		log.debug("saving Mom Instance");
		try{
			getHibernateTemplate().save(instance);
			log.debug("saving successful");
		}catch(RuntimeException re){
			log.debug("saving fail",re);
			throw re;
		}
	}

	@Override
	public void delete(DataContents instance) {
		log.debug("deleting Mom Instance");
		try{
			getHibernateTemplate().delete(instance);
			log.debug("delete successful");
		}catch(RuntimeException re){
			log.debug("delete fail",re);
			throw re;
		}
	}
	
	@Override
	public void update(DataContents instance) {
		
		log.debug("update Mom Instance");
		try{
			getHibernateTemplate().update(instance);
			
		}catch(RuntimeException re){
			log.error("update failed!",re);
			throw re;
		}	
	}

	@Override
	public List<DataContents> findByCondition(DataContentsModel model) {
		log.debug("findByCondition Mom Instance");
		StringBuffer queryString = new StringBuffer("from DataContents m where 1=1");
//		if(!StringUtils.isEmpty(model.getMid())){
//			queryString.append("and m.mid = '" + model.getMid() + "'" );
//		}
//		if(!StringUtils.isEmpty(model.getAsid())){
//			queryString.append("and m.asid = '" + model.getAsid() + "'" );
//		}
		queryString.append(" order by m.sentTime desc");
		return getHibernateTemplate().find(queryString.toString());
	}

	
	
	@Override
	public List<Object> findByCondition(DataContentsModel model,String userName, Pager p) {
	    log.debug("finding all findByCondition instances");
		Session session = null;
		Query query = null;
		try {
			StringBuffer queryString = new StringBuffer("from DataContents m where 1=1");
			if(model.getMachines() != null){
				if(!StringUtils.isEmpty(model.getMachines().getName())){
					queryString.append("and m.machines.name = '" + model.getMachines().getName() + "'" );
				}
				if(!StringUtils.isEmpty(model.getMachines().getDeviceCode())){
					queryString.append(" and m.machines.deviceCode = '" + model.getMachines().getDeviceCode() + "'" );
				}
			}
			queryString.append(" and m.machines.userName = '"+userName+"' " );
			if(model.getAppleServer() != null){
				if(!StringUtils.isEmpty(model.getAppleServer().getName())){
					queryString.append(" and m.appleServer.name = '" + model.getAppleServer().getName() + "'" );
				}
			}
			queryString.append(" order by m.sentTime asc");
			session = getSessionFactory().openSession();
			query = session.createQuery(queryString.toString());
			List<Object> resList = new ArrayList();
			resList.add(query.list().size());
			setQueryPager(query, p);
			resList.add(query.list());
			return resList;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			closeSessionAndQuery(session, query);
		}
	}
	
	@Override
	public List<Object> findByConditionAp(DataContentsModel model,String userName, Pager p) {
	    log.debug("finding all findByCondition instances");
		Session session = null;
		Query query = null;
		try {
			StringBuffer queryString = new StringBuffer("from DataContents m where 1=1");
			if(model.getMachines() != null){
				if(!StringUtils.isEmpty(model.getMachines().getName())){
					queryString.append("and m.machines.name = '" + model.getMachines().getName() + "'" );
				}
				if(!StringUtils.isEmpty(model.getMachines().getDeviceCode())){
					queryString.append(" and m.machines.deviceCode = '" + model.getMachines().getDeviceCode() + "'" );
				}
			}
			
			if(model.getAppleServer() != null){
				if(!StringUtils.isEmpty(model.getAppleServer().getName())){
					queryString.append(" and m.appleServer.name = '" + model.getAppleServer().getName() + "'" );
				}
			}
			queryString.append(" and m.appleServer.userName = '"+userName+"' " );
			queryString.append(" order by m.sentTime asc");
			session = getSessionFactory().openSession();
			query = session.createQuery(queryString.toString());
			List<Object> resList = new ArrayList();
			resList.add(query.list().size());
			setQueryPager(query, p);
			resList.add(query.list());
			return resList;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}finally{
			closeSessionAndQuery(session, query);
		}
	}
}
